#pragma comment(linker, "/STACK:10000000")
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <set>
#include <map>
#include <cstdio>
#include <cassert>
#include <string>
#include <cstring>
#define ldb long double
#define LL long long
#define fi first
#define se second
#define fill(a, c) memset(a, c, sizeof(a))
#define sqr(a) ((a) * (a))
#define nextLine() {int c = 0; while((c = getchar()) != 10 && c != EOF);}
#define getBit(mask, k) (((mask) / pw[k]) % pw[1])
#define setBit(mask, k, l) (((mask) / pw[k + 1] * pw[1] + (l)) * pw[k] + ((mask) % pw[k]))
#define debug(a) cerr << #a << " = " << a << " ";
#define debugl(a) cerr << #a << " = " << a << "\n";
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define ff first
#define ss second 
const ldb LDINF = 9128739847123.00;
const ldb eps = 1e-9;
const int inf = 1 << 28;
const ldb pi = fabsl(atan2l(0.0, -1.0));
using namespace std;

int n;

int summator[11][4][11][21][6][2][91];
//int add[11][4][11][21][6][2][91];
const int cnt_regions = 6;
const int cnt_products = 11;
const int cnt_sizes = 4;
const int cnt_provinces = 11;
const int cnt_cities = 21;

inline int getCnt(int szid, int city, int reg)
{
	return max(1, (szid == cnt_sizes - 1) * (cnt_sizes - 1)) *
	 max((city == cnt_cities - 1) * (cnt_cities - 1), 1)
	 * max(1, (reg == cnt_regions - 1) * (cnt_regions - 1));
}


void addv(int prodid, int szid, int prov, int city, int reg, int sex, int age, int cnt)
{
#ifndef ONLINE_JUDGE                     	
	cerr << "add " << prodid << " size " << szid << " province " << prov << " city " << city << " region " << reg << " sex " 
	<< sex << " age " << age << " add " << cnt << "\n"; 
#endif
	city = (city + cnt_cities) % cnt_cities;
	reg = (reg + cnt_regions) % cnt_regions;
	szid = (szid + cnt_sizes) % cnt_sizes;
	int i = age;
	while (i <= 90)
	{
		summator[prodid][szid][prov][city][reg][sex][i] += cnt;
		if (reg < cnt_regions - 1)
			summator[prodid][szid][prov][city][cnt_regions - 1][sex][i] += cnt;
		if (city < cnt_cities - 1)
			summator[prodid][szid][prov][cnt_cities - 1][cnt_regions - 1][sex][i] += cnt;
		if (szid < cnt_sizes - 1)
		{
			summator[prodid][cnt_sizes - 1][prov][city][reg][sex][i] += cnt;
			if (reg < cnt_regions - 1)
				summator[prodid][cnt_sizes - 1][prov][city][cnt_regions - 1][sex][i] += cnt;
			if (city < cnt_cities - 1)
				summator[prodid][cnt_sizes - 1][prov][cnt_cities - 1][cnt_regions - 1][sex][i] += cnt;
		}
//		if (szid == cnt_cities - 1 || reg == cnt_regions - 1 || city == cnt_cities - 1)
//		add[prodid][szid][prov][city][reg][sex][i] += cnt;
		i |= (i + 1);
	}
}
int getSum(int prodid, int szid, int prov, int city, int reg, int sex, int age)
{
	if (age < 0) return 0;
	city = (city + cnt_cities) % cnt_cities;
	reg = (reg + cnt_regions) % cnt_regions;
	szid = (szid + cnt_sizes) % cnt_sizes;
	int i = age;
	int res = 0;
	while (i >= 0)
	{
		res += summator[prodid][szid][prov][city][reg][sex][i];
		/*if (reg < cnt_regions - 1)
			res += add[prodid][szid][prov][city][cnt_regions - 1][sex][i];
		else if (city < cnt_cities - 1)
			res += add[prodid][szid][prov][cnt_cities - 1][cnt_regions - 1][sex][i];
		if (szid < cnt_sizes - 1)
		{
			res += add[prodid][cnt_sizes - 1][prov][city][reg][sex][i];
			if (reg < cnt_regions - 1)
				res += add[prodid][cnt_sizes - 1][prov][city][cnt_regions - 1][sex][i];
			else if (city < cnt_cities - 1)
				res += add[prodid][cnt_sizes - 1][prov][cnt_cities - 1][cnt_regions - 1][sex][i];
		}
		*/
//		if (szid == cnt_cities - 1 || reg == cnt_regions - 1 || city == cnt_cities - 1)
		//add[prodid][szid][prov][city][reg][sex][i] += cnt;
		i = (i & (i + 1)) - 1;
	}
	return res;
}

int query(int prodid, int szid, int prov, int city, int reg, int sex, int sage, int eage)
{
#ifndef ONLINE_JUDGE                     	
	cerr << "query " << prodid << " " << szid << " " << prov << " " << city << " " << reg << " " << sex << " " << sage << " " << eage << "\n"; 
#endif
	if (eage == -1) eage = sage;
	int f1, t1;
	int f2, t2;
	f1 = prodid;
	t1 = prodid + 1;
	f2 = prov;
	t2 = prov + 1;
	if (prodid == -1)
	{
		f1 = 0;
		t1 = cnt_products - 1;
	}
	if (prov == -1)
	{
		f2 = 0;
		t2 = cnt_provinces - 1;
	}
	int r = 0;
	for (int i = f1; i < t1; i++)
	{
		for (int j = f2; j < t2; j++)
		{
			r += getSum(i, szid, j, city, reg, sex, eage) - getSum(i, szid, j, city, reg, sex, sage - 1);
		}
	}
	return r;
}

void load()
{
	cin >> n;
	int product_id , size_id , province , city_id , region_id , age , units_sold, sex, start_age, end_age;
	int c;
	for (int i= 0; i < n; i++)
	{
		while ((c = getchar()) != 'I' && c != 'Q');
		if (c == 'I')
		{
			while (isspace(c = getchar()));
			product_id = size_id = province = city_id = region_id = age = units_sold = -1;
			product_id = 0;
			while (c >= '0' && c <= '9')
			{
				product_id = product_id * 10 + c - '0';
				c = getchar();
			}
			if (c == '.')
			{
				size_id = 0;
				while ((c = getchar()) >= '0' && c <= '9')
                	size_id = size_id * 10 + c - '0';
            }
            while (isspace(c)) c = getchar();
            province = 0;
            while (c >= '0' && c <= '9')
            {
            	province = province * 10 + c - '0';
            	c = getchar();
           	} 
           	if (c == '.')
           	{
           		city_id = 0;
           		while ((c = getchar()) >= '0' && c <= '9')
           			city_id = city_id * 10 + c - '0';
           		if (c == '.')
           		{
           			region_id = 0;
           			while ((c = getchar()) >= '0' && c <= '9')
           				region_id = region_id * 10 + c - '0';
           		}
           	}
           	while (c != 'M' && c != 'F') c = getchar();
           	sex = (c == 'M');
           	while (isspace(c = getchar()));
           	age = 0;
           	while (c >= '0' && c <= '9')
           	{
           		age = age * 10 + c - '0';
           		c = getchar();
           	}
           	units_sold = 0;
           	while ((c = getchar()) >= '0' && c <= '9')
           		units_sold = units_sold * 10 + c - '0';
           	addv(product_id, size_id, province, city_id, region_id, sex, age, units_sold);
		}
		else
		{
			while (isspace(c = getchar()));
			product_id = size_id = province = city_id = region_id = age = start_age = end_age = -1;
			int sign = 1;
			if (c == '-')
			{
				sign = -1;
				c = getchar();
			}
			product_id = 0;
			while (c >= '0' && c <= '9')
			{
				product_id = product_id * 10 + c - '0';
				c = getchar();
			}
			product_id *= sign;
			if (c == '.')
			{
				size_id = 0;
				while ((c = getchar()) >= '0' && c <= '9')
                	size_id = size_id * 10 + c - '0';
            }
            while (isspace(c)) c = getchar();
            sign = 1;
            province = 0;
            if (c == '-')
            {
            	sign = -1;
            	c = getchar();
            }
            while (c >= '0' && c <= '9')
            {
            	province = province * 10 + c - '0';
            	c = getchar();
           	} 
           	province *= sign;
           	if (c == '.')
           	{
           		city_id = 0;
           		while ((c = getchar()) >= '0' && c <= '9')
           			city_id = city_id * 10 + c - '0';
           		if (c == '.')
           		{
           			region_id = 0;
           			while ((c = getchar()) >= '0' && c <= '9')
           				region_id = region_id * 10 + c - '0';
           		}
           	}
           	while (c != 'M' && c != 'F') c = getchar();
           	sex = (c == 'M');
           	while (isspace(c = getchar()));
           	start_age = 0;
           	while (c >= '0' && c <= '9')
           	{	
           		start_age = start_age * 10 + c - '0';
           		c = getchar();
           	}
           	if (c == '-')
           	{
           		end_age = 0;
	           	while ((c = getchar()) >= '0' && c <= '9')
   	        		end_age = end_age * 10 + c - '0';
   	        }
           	printf("%d\n", query(product_id, size_id, province, city_id, region_id, sex, start_age, end_age));
		}
	}
}

void solve()
{

}

#define file "baking"
int main()
{
	#ifndef ONLINE_JUDGE
		freopen(file".in", "rt", stdin);
		freopen(file".out", "wt", stdout);
	#endif
	load();
	solve();
	return 0;
}

